package com.hospital.new_yygh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hospital.new_yygh.service.PatientService;
import com.hospital.new_yygh.entity.user.Patient;
import com.hospital.new_yygh.entity.result.Result;
import com.wxapi.WxApiCall.WxApiCall;
import com.wxapi.model.RequestModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Api(tags = "预约挂号系统")
@RestController
@RequestMapping("/patient")
@CrossOrigin
public class PatientController {

    //注入service
    @Autowired
    private PatientService patientService;

    //患者登录接口
    @ApiOperation(value = "患者登录")
    @PostMapping("/patientLogin/{username}/{password}")
    public Result patientLogin(@PathVariable String username,
                               @PathVariable String password){
        QueryWrapper<Patient> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        Patient patient = patientService.getOne(wrapper);
        //查询管理员是否存在
        if (patient != null && patient.getPassword().equals(password)){
            //管理员存在且密码正确
            return Result.ok(patient);
        }else{
            return Result.fail();
        }
    }

    //根据id获取患者信息
    @ApiOperation(value = "根据id查询患者")
    @GetMapping("getPatient/{id}")
    public Result getPatient(@PathVariable Long id){
        Patient patient = patientService.getById(id);
        return Result.ok(patient);
    }

    //修改患者信息
    @ApiOperation(value = "修改患者信息")
    @PostMapping("updatePatient")
    public Result updatePatient(@RequestBody Patient patient){
        boolean b = patientService.updateById(patient);
        if (b){
            return Result.ok();
        }else{
            return Result.fail();
        }
    }

    //智能分诊
    @ApiOperation(value = "修改患者信息")
    @PostMapping("target/{name}/{age}/{sex}/{description}")
    public Result target(@PathVariable String name,
                         @PathVariable int age,
                         @PathVariable String sex,
                         @PathVariable String description){
        System.out.println(name+" "+age+" "+sex+" "+description);

        RequestModel model = new RequestModel();
        model.setGwUrl("https://aiapi.jd.com/jdai/intelligent_triage");
        model.setAppkey("f1158f3e7629310b205ac71fda8ad3eb");
        model.setSecretKey("8bd197d133e3b9e55a2570cfb3d0de63");
        model.setBodyStr("{\""+"description\""+":"+"\""+description+"\""+","+"\"patientInformation\""+":{\"name\":"+"\""+name+"\""+",\"age\":"+"\""+age+"\""+",\"sex\":"+"\""+sex+"\""+"}}");
        Map queryMap = new HashMap();
        queryMap.put("x-ai-request-id","2f21e3de77ba4b4ab03cadb823ad145c");//访问参数
        model.setQueryParams(queryMap);
        WxApiCall call = new WxApiCall();
        call.setModel(model);
        String res = call.request();
        System.out.println(res);
        return Result.ok(res);
    }
}
